<script setup>
import PageContainer from '@/components/pageContainer.vue'
import {
  getFoodRecipeListService,
  exportAllRecipeInfo,
  deleteFoodRecipeService
} from '@/api/foodRecipeDetails/foodRecipeDetails'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const queryParams = ref({
  page: 1,
  pageSize: 8,
  name: null
})
const foodRecipeList = ref([])
const page = ref(1)
const pageSize = ref(6)
const foodRecipeName = ref('')
const total = ref(0)
const loading = ref(false)
//获取食谱明细
const getFoodRecipeList = async () => {
  const res = await getFoodRecipeListService(queryParams.value)
  foodRecipeList.value = res.data.data.records
  total.value = res.data.data.total
}

//删除食谱
const deleteFoodRecipe = (row) => {
  ElMessageBox.confirm('确认要删除该食谱吗', '提示', {
    customClass: 'el-message-box-myself',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    confirmButtonClass: 'ElMessageBox-confirm-button-style',
    cancelButtonClass: 'ElMessageBox-cancel-button-style'
  })
    .then(async () => {
      console.log(row.id)
      const res = await deleteFoodRecipeService(row.id)
      if (res.data.code === 1) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        await getFoodRecipeList()
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消'
      })
    })
}

//下载表格
const downloadExcel = async () => {
  ElMessage({
    type: 'info',
    message: '"导出中...请等待"'
  })
  loading.value = true

  await exportAllRecipeInfo().then((res) => {
    loading.value = false
  })
}
const handleQuery = () => {
  queryParams.value.page = 1
  getFoodRecipeList()
}
const resetForm = () => {
  queryParams.value.name = null
  handleQuery()
}

//在页面初次渲染执行
onMounted(() => {
  getFoodRecipeList()
})
</script>

<template>
  <div class="floating-box">
    <div>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="68px"
        class="no-wrap"
        @keydown.enter.prevent
      >
        <el-form-item label="食谱名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入食谱名称"
            prefix-icon="Search"
            style="width: 320px"
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item>
          <div class="no-wrap">
            <el-button
              icon="Search"
              plain
              round
              color="#95d475"
              @click="handleQuery()"
              >搜索</el-button
            >
            <el-button
              icon="Refresh"
              plain
              round
              color="#95d475"
              @click="resetForm()"
              >重置</el-button
            >
            <el-button
              icon="Download"
              plain
              round
              color="#95d475"
              @click="downloadExcel()"
              >导出食谱</el-button
            >
          </div>
        </el-form-item>
      </el-form>
      <el-divider />
    </div>

    <el-table
      :data="foodRecipeList"
      width="100%"
      :stripe="true"
      size="large"
      v-loading="loading"
    >
      <el-table-column
        label="食谱ID"
        prop="id"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="谷物组"
        prop="grains"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="谷物组重量"
        prop="grainsWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="蔬菜组"
        prop="vegetable"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="蔬菜组重量"
        prop="vegetableWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="肉组"
        prop="meat"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="肉组重量"
        prop="meatWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="水果组"
        prop="fruit"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="水果组重量"
        prop="fruitWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="乳类"
        prop="milkGroup"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="乳类组重量"
        prop="milkGroupWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="脂肪组"
        prop="grease"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="脂肪组重量"
        prop="greaseWeight"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="{ row }">
          <el-button
            type="info"
            plain
            round
            icon="Delete"
            @click="deleteFoodRecipe(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        v-show="total > 0"
        :total="total"
        @size-change="getFoodRecipeList"
        @current-change="getFoodRecipeList"
      />
    </div>
  </div>
</template>

<style scoped>
/deep/ .el-input__wrapper {
  border-radius: 50px;
  /*background-color: red;*/
}
</style>
